package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Sku;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.pojo.CartItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private TokenDecode tokenDecode;

    @Autowired
    private CartService cartService;

    @Autowired
    private HttpServletRequest request;

    @PostMapping("/add")
    public Result add(@RequestParam("skuId")String skuId, @RequestParam("num")Integer num) throws UnsupportedEncodingException {
        String userName = getUserName();
        cartService.add(skuId,num,userName);
        return new Result(true, StatusCode.OK,"加入购物车成功");
    }

    /**
     * 获取购物车列表
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping("/list")
    public Result<Map<String,List<CartItem>>> list() throws UnsupportedEncodingException {

        String userName = getUserName();
        Map<String,List<Map>> cart = cartService.getListFromRedis(userName);
        return new Result<Map<String,List<CartItem>>>(true,StatusCode.OK,"查询成功",cart);
    }

    /**
     * 删除对应skuid的购物车
     * @param skuId
     * @return
     */
    @DeleteMapping("/{skuId}")
    public Result deleteCart(@PathVariable("skuId") String skuId){
        String userName = getUserName();
        cartService.delete(skuId,userName);
        return new Result(true, StatusCode.OK,"删除购物车成功");
    }


    /**
     * 修改购物车数量
     * @return
     */
    @PutMapping("/changeNum/{skuId}/{num}")
    public Result changeNum(@PathVariable("skuId")String skuId,@PathVariable("num")Integer num){
        String userName = getUserName();
        cartService.updateNum(skuId,num,userName);
        return new Result(true, StatusCode.OK,"修改购物车成功");
    }

    /**
     * 修改购物车状态
     * @return
     */
    @PutMapping("/changeChecksd/{skuId}")
    public Result changeChecksd(@PathVariable("skuId")String skuId) throws UnsupportedEncodingException {
        String userName = getUserName();
        cartService.updateChecksd(skuId,userName);
        return new Result(true, StatusCode.OK,"修改购物车成功");
    }
    /**
     * 修改购物车状态
     * @return
     */
    @PutMapping("/allChecksd/{checikStatus}")
    public Result allChecksd(@PathVariable("checikStatus")Boolean checikStatus) throws UnsupportedEncodingException {
        String userName = getUserName();
        cartService.allChecksd(checikStatus,userName);
        return new Result(true, StatusCode.OK,"修改购物车成功");
    }
    /**
     * 获取用户名
     * @return
     */
    public String getUserName(){
        String authorization = request.getHeader("Authorization");
        if (!StringUtils.isEmpty(authorization)){
            String userName = tokenDecode.getUserName();
            return userName;
        }
        return null;
    }

}
